package com.website.bookshelf.service;

import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

@Service
@RequiredArgsConstructor
public class EmailService {
    private final JavaMailSender javaMailSender;
    private final TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String fromMail;

    public void sendVerificationEmail(String to) throws MessagingException {
        Context context = new Context();
        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setFrom(fromMail);
        helper.setTo(to);
        helper.setSubject("Bookshelf, email confirmation");

        String template = templateEngine.process("email-verification.html", context);
        helper.setText(template, true);

        ClassPathResource logo = new ClassPathResource("static/img/bookshelf-logo.png");
        ClassPathResource email = new ClassPathResource("static/img/email.png");
        helper.addInline("bookshelf-logo.png", logo, "image/x-icon");
        helper.addInline("email.png", email, "image/x-icon");

        javaMailSender.send(message);
    }
}
